package com.marktotop.file.controller;

import com.marktotop.file.entity.LoginDTO;
import com.marktotop.file.entity.TbUser;
import com.marktotop.file.response.ResultBean;
import com.marktotop.file.response.ResultCode;
import com.marktotop.file.service.LoginService;
import com.marktotop.file.service.TbUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.Map;

/**
 * @author WXLD
 * @data 2021/4/19 - 15:39
 */
@CrossOrigin
@RestController
@Api(tags = "系统登录接口")
@Slf4j(topic = "login")
@RequestMapping(value = "/api")
public class LoginController {

    @Autowired
    private LoginService loginService;

    int Success = 2;
    int ErrorPwd = 1;

    @ApiOperation(value = "用户登录",notes="POST提交，返回登录结果")
    @RequestMapping(value = "/login",method = RequestMethod.POST)
    public ResultBean login(@RequestBody LoginDTO loginUser){
        String username = loginUser.getUsername();
        String password = loginUser.getPassword();
        int loginCode = loginService.login(username,password);
        if (loginCode != Success){
            if(loginCode == ErrorPwd){
                return ResultBean.result(ResultCode.LOGIN_PASSWORD_ERROR);
            }
            return ResultBean.result(ResultCode.LOGIN_USER_NOT_FOUND);
        }
        return ResultBean.ok().data("secretKey",username);
    }


    @ApiOperation(value = "获取用户信息",notes="GET提交，返回用户信息")
    @RequestMapping(value = "/profile",method = RequestMethod.GET)
    public ResultBean getProfile(@RequestParam("username") String username){
        TbUser userInfo = loginService.getUserProfile(username);
        if (userInfo != null){
            return ResultBean.ok().data("user",userInfo);
        }
        return ResultBean.error();
    }
}
